package com.ruoyi.web.controller.warning;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.domain.warning.WarningFactorRecord;
import com.ruoyi.web.domain.warning.WarningInsectRecord;
import com.ruoyi.web.pojo.request.warning.WarningFactorRecordRequest;
import com.ruoyi.web.pojo.request.warning.WarningInsectRecordRequest;
import com.ruoyi.web.pojo.request.warning.WarningRecordCountByDayRequest;
import com.ruoyi.web.pojo.vo.warning.HomeWarningStatisticsVo;
import com.ruoyi.web.pojo.vo.warning.WarningRecordCountByDayVO;
import com.ruoyi.web.pojo.vo.warning.WarningRecordCountVO;
import com.ruoyi.web.service.warning.WarningFactorRecordService;
import com.ruoyi.web.service.warning.WarningInsectRecordService;
import com.ruoyi.web.utils.v2.V2DeviceInfo;
import com.ruoyi.web.utils.v2.V2RremoteCall;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(value = "预警记录",tags = "预警记录")
@RestController
@RequestMapping("/warning-record")
public class WarningRecordController extends BaseController {


    @Autowired
    private WarningFactorRecordService warningFactorRecordService;

    @Autowired
    private WarningInsectRecordService warningInsectRecordService;

    @ApiOperation("分页获取-传感器-预警记录")
    @GetMapping("/queryPageFactorRecord")
    public TableDataInfo<WarningFactorRecord> queryPageFactorRecord(WarningFactorRecordRequest request){
        List<WarningFactorRecord> warningAddressBooks = warningFactorRecordService.queryPage(request);
        return getDataTable(warningAddressBooks);
    }

    @ApiOperation("根据条件-传感器-预警记录")
    @GetMapping("/getFactorRecordByInfo")
    public  R<List<WarningFactorRecord>>  getFactorRecordByInfo(WarningFactorRecordRequest request){
        List<WarningFactorRecord> records = warningFactorRecordService.getLastRecord(request);
        return R.ok(records);
    }

    @GetMapping("/getFactorRecord")
    @ApiOperation("根据id获取-传感器-预警记录")
    public R<WarningFactorRecord> getFactorRecord(@RequestParam Long id) {
        WarningFactorRecord record = warningFactorRecordService.getById(id);
        V2DeviceInfo deviceInfo = V2RremoteCall.getById(record.getDeviceId());
        record.setDeviceInfo(deviceInfo);
        return R.ok(record);
    }

    @ApiOperation("分页获取-虫情-预警记录")
    @GetMapping("/queryPageInsectRecord")
    public TableDataInfo<WarningInsectRecord> queryPageFactorRecord(WarningInsectRecordRequest request){
        List<WarningInsectRecord> warningInsectRecords = warningInsectRecordService.queryPage(request);
        return getDataTable(warningInsectRecords);
    }

    @GetMapping("/getInsectRecord")
    @ApiOperation("根据id获取-虫情-预警记录")
    public R<WarningInsectRecord> getInsectRecord(@RequestParam Long id) {
        WarningInsectRecord record = warningInsectRecordService.getById(id);
        V2DeviceInfo deviceInfo = V2RremoteCall.getById(record.getDeviceId());
        record.setDeviceInfo(deviceInfo);
        return R.ok(record);
    }


    @GetMapping("/getRecordCount")
    @ApiOperation("首页-获取预警统计结果")
    public R<WarningRecordCountVO> getRecordCount() {
        WarningRecordCountVO countVO = warningInsectRecordService.getRecordCount();
        return R.ok(countVO);
    }

    @GetMapping("/getRecordCountByDay")
    @ApiOperation("首页-获取一段时间内每一天的预警统计")
    public R<List<WarningRecordCountByDayVO>> getRecordCountByDay(WarningRecordCountByDayRequest request) {
        List<WarningRecordCountByDayVO> list = warningInsectRecordService.getRecordCountByDay(request);
        return R.ok(list);
    }

    @GetMapping("/getHomeWarningStatistics")
    @ApiOperation("首页-获取右下角的预警统计")
    public R<HomeWarningStatisticsVo> getHomeWarningStatistics(@RequestParam(value = "dateType",defaultValue = "1") Integer dateType) {
        HomeWarningStatisticsVo list = warningInsectRecordService.getHomeWarningStatistics(dateType);
        return R.ok(list);
    }

}
